import Vue from 'vue'
import VueRouter from 'vue-router'

// import Order from '../components/order/Order.vue'
const Order = () => import('../components/order/Order.vue')
// import Report from '../components/report/report.vue'
const Report = () => import('../components/report/report.vue')
// import GoodsList from '../components/goods/List.vue'
const GoodsList = () => import('../components/goods/List.vue')
// import Add from '../components/goods/add.vue'
const Add = () => import('../components/goods/add.vue')
// import Edit from '../components/goods/edit.vue'
const Edit = () => import('../components/goods/edit.vue')
// import Cate from '../components/goods/Cate.vue'
const Cate = () => import('../components/goods/Cate.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import('../components/goods/Params.vue')
// import Rights from '../components/power/Rights.vue'
const Rights = () => import('../components/power/Rights.vue')
// import Roles from '../components/power/Roles.vue'
const Roles = () => import('../components/power/Roles.vue')
// import Users from '../components/user/Users.vue'
const Users = () => import('../components/user/Users.vue')
// import Login from '../components/Login.vue'
const Login = () => import('../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import('../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import('../components/Welcome.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'Login_home_welcome', component: Login },
  {
    path: '/home',
    component: Home,
    name: 'Login_home_welcome',
    children: [
      {
        path: '',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: Welcome,
        name: 'Login_home_welcome'
      },
      {
        path: '/user',
        name: 'User_Rights_Roles',
        component: Users
      },
      {
        path: '/rights',
        name: 'User_Rights_Roles',
        component: Rights
      },
      {
        path: '/roles',
        name: 'User_Rights_Roles',
        component: Roles
      },
      {
        path: '/categories',
        name: 'Cate_Params',
        component: Cate
      },
      {
        path: '/params',
        name: 'Cate_Params',
        component: Params
      },
      {
        path: '/goods',
        name: 'Goods_Add_Edit',
        component: GoodsList
      },
      {
        path: '/goods/add',
        name: 'Goods_Add_Edit',
        component: Add
      },
      {
        path: '/goods/edit',
        name: 'Goods_Add_Edit',
        component: Edit
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Order
      },
      {
        path: '/report',
        name: 'Report',
        component: Report
      }
    ]
  }
  // children: [
  //   { path: '', redirect: '/welcome' },
  //   { path: '/welcome', name: 'Login_home_welcome', component: Welcome },
  //   { path: '/user', component: Users },
  //   { path: '/rights', component: Rights },
  //   { path: '/roles', component: Roles },
  //   { path: '/categories', component: Cate },
  //   { path: '/params', component: Params },
  //   { path: '/goods', component: GoodsList },
  //   { path: '/goods/add', component: Add },
  //   { path: '/goods/edit', component: Edit },
  //   { path: '/orders', component: Order },
  //   { path: '/report', component: Report }
  // ]
]

const router = new VueRouter({
  routes
})
//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //  to将要访问的路径，from代表从哪个路径跳转而来，next是一个函数，表示放行，next('/login')强制跳转
  if (to.path === '/login') return next()
  //  获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
